<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Review extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'review';

    /**
     * @var array
     */
    protected $fillable = [
        'user_id',
        'room_check_in_id',
        'review',
        'images',
        'service_rating',
        'health_rating',
        'facilities_rating',
        'network_rating',
    ];

    public static function add($data)
    {
        $data['create_at'] = $data['updated_at'] = date('Y-m-d H:i:s');
        $data['images'] = json_encode($data['images'], JSON_UNESCAPED_UNICODE);
        try {
            return self::create($data);
        } catch (\Exception $exception) {
            return $exception->getMessage();
        }
    }

    public function getCountByUser($userId, $roomCheckId)
    {
        return DB::table($this->table)
            ->where('user_id', $userId)
            ->where('room_check_in_id', $roomCheckId)
            ->count();

    }

}
